package com.extension.company.admin.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.extension.company.KEY;
import com.extension.company.common.utils.ToastUtil;
import com.extension.company.view.home.hotactivity.OnlineWaitForPayActivity;
import com.extension.company.view.home.hotactivity.PaySuccessActivity;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * 作者：zwk
 * 时间：on 2019/3/21 15:59
 * 功能：微信支付的回调的功能
 */
public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
	private static final String TAG = WXPayEntryActivity.class.getSimpleName();
	private IWXAPI api;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		api = WXAPIFactory.createWXAPI(this, KEY.WX_PAY_ID);
		api.handleIntent(getIntent(), this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq baseReq) {

	}

	@Override
	public void onResp(BaseResp baseResp) {
		if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if (baseResp.errCode == 0) {
//				ToastUtil.showToast(this, "支付成功");
				Intent intent = new Intent(this, PaySuccessActivity.class);
				startActivity(intent);
				finish();
			} else if (baseResp.errCode == 2) {
				ToastUtil.showToast(this, "取消支付");
			} else {
				ToastUtil.showToast(this, "支付失败");
			}

		}

	}


}
